$(function () {
    var layer = layui.layer;
    var form = layui.form;
    var laypage = layui.laypage;
    //----------------------------------------------------------------
    //  页面查询参数
    var query = {
        pagenum: 1, //页码值
        pagesize: 10, //每页显示几条数据
        device_name: '', //设备名称
        device_serial: '', //设备序列号
    }
    // 渲染分页功能
    function renderPage(total) {
        // console.log(total);
        laypage.render({
            elem: 'pageBox', //分页容器的id
            count: total, //数据总数，从服务端得到
            limit: query.pagesize, //每页显示几条数据
            curr: query.pagenum,
            layout: ["count", "limit", "prev", "page", "next", "skip"],
            limits: [10, 12, 15, 20],
            jump: function (obj, first) {
                // console.log(first);
                query.pagenum = obj.curr;
                query.pagesize = obj.limit;
                // 首行不执行
                if (!first) {
                    initRepairList();
                }
            },

        });
    }
    //------------------------------------------------------------------------------
    // 调用渲染设备列表信息的函数
    initRepairList();
    //------------------------------------------------------------------------------
    // 获取所有报废实验室设备的列表
    function initRepairList() {
        var device_name = $('#form_search [name = device_name ]').val()
        var device_serial = $('#form_search [name = device_serial ]').val()
        // console.log(device_name);
        // console.log(device_serial);
        query.device_name = device_name;
        query.device_serial = device_serial;
        $.ajax({
            method: 'GET',
            url: '/my/deviceManage/repairDeviceList/',
            data: query,
            success: function (res) {
                // console.log(res);
                var htmStr = template('tpl_repair_device_list', res);
                $('tbody').html(htmStr)
                renderPage(res.total)
            }
        })
    }
    //----------------------------------------------------------------
    //模糊搜索需要的信息
    $('#form_search').on('submit', function (e) {
        e.preventDefault()
        initRepairList();
    })
    //----------------------------------------------------------------
    // 根据id查看设备图片
    $('tbody').on('click', '.btn_viewDevice', function () {
        // console.log('ok');
        var id = $(this).attr('data_id');
        // console.log(id);
        $.ajax({
            method: 'GET',
            url: '/my/deviceManage/viewRepairDeviceLog/' + id,
            success: function (res) {
                if (res.status !== 0) {
                    return layer.msg(res.message)
                }
                var htmStr = template('tpl_view_device', res.data);
                console.log(htmStr);
                $('[name="view_device').html(htmStr);
            }
        })

        viewDevice_index = layer.open({
            type: 1,
            area: ['800px', '600px'],
            title: '查看设备图片',
            content: $('#tpl_view_device').html(),
            success: function () {
                form.render();
            }
        })

    })
})